package pro.cherish.smartfile.config.properties;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.io.File;
import java.util.Date;

/**
 * @author 创建人：lys
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2020-5-27 lys 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：BizProperties
 * @Description 功能说明：业务配置
 * <p>
 *
 * </p>
 * **********************************************************************
 * @date 创建日期： 2020-5-27
 */
@ConfigurationProperties(
        prefix = "file.config"
)
@Configuration
@Getter
@Setter
@Component
@Validated
public class SysProperties {

    private String rootPath = "O://temp/smart_file";

    private String redisRoot = "smart:file";
    /**
     * 单位天
     */
    private Integer taskDuration = 30;

    private Integer chunkSize = 10000000;
//    private Integer chunkSize = 1024;

    /**
     * 获取redis根路径
     *
     * @return
     */
    public String getRedisRoot() {
        return this.redisRoot;
    }


    /**
     * 获取归档绝对根路径
     *
     * @param targetDate
     * @return
     */
    public String getArchivePath(Date targetDate) {
        return rootPath + File.separator + "archive" + File.separator + DateUtil.year(targetDate) + File.separator + DateUtil.month(targetDate) + File.separator + DateUtil.formatDate(targetDate);
    }

    /**
     * 临时目录根路径
     *
     * @return
     */
    public String getTempRootPath() {
        return rootPath + File.separator + "temp";
    }

    /**
     * 获取绝对临时文件路径，不存在会创建
     *
     * @param taskId
     * @return
     */
    public File getTempRootPath(String taskId) {
        File file = new File(getTempRootPath() + File.separator + taskId);
        if (!FileUtil.exist(file)) {
            FileUtil.mkdir(file);
        }
        return file;
    }


    /**
     * 获取绝对归档文件路径
     *
     * @param targetDate
     * @return
     */
    public File getArchivePath(Date targetDate, String taskId) {
        File file = new File(getArchivePath(targetDate) + File.separator + taskId);
        if (!FileUtil.exist(file)) {
            FileUtil.mkdir(file);
        }
        return file;
    }
}
